﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.IO;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Bitis_KeToanThue_Pro_QuanTri.AccessDatabase;

namespace Bitis_KeToanThue_Pro_QuanTri.Form
{
    public partial class frmDanhMucNguoiDung : Telerik.WinControls.UI.RadForm
    {
        DanhMucNguoiDung dm = new DanhMucNguoiDung();
        public static string option = "";
        public frmDanhMucNguoiDung()
        {
            InitializeComponent();
            khoitaogrid();
            ClassXuLyChung.loadcombodungchung(cbPhongBan, dm.loadcombophongban(), "TenPB", "MaPB");
            ClassXuLyChung.loadcombodungchung(cbChucVu, dm.loadcombochucvu(), "ChucVu", "MaChucVu");
            ClassXuLyChung.loadcombodungchung(cbDoUuTien, dm.loadcombodouutien(), "DienGiai", "DoUuTien");
            loadgrid();
            trangthaisudungcontrol(false);
            trangthaisudung2button(true);
            trangthaisudung3button(false);
            UpdatePanelInfo(this.radGridView1.CurrentRow);
            if (BienHeThong.ht_quyencapnhat == 1)
            {
                btnSua.Enabled = true;
                btnThem.Enabled = true;
            }
            else
            {
                btnSua.Enabled = false;
                btnThem.Enabled = false;
            }
            if (BienHeThong.ht_quyenxoa == 1)
            {
                btnXoa.Enabled = true;
            }
            else
            {
                btnXoa.Enabled = false;
            }
        }
        private void UpdatePanelInfo(GridViewRowInfo currentRow)
        {
            if (currentRow != null && !(currentRow is GridViewNewRowInfo))
            {

                txtUserID.Text = ClassXuLyChung.GetSafeString(currentRow.Cells[0].Value);
                txtMaNV.Text = ClassXuLyChung.GetSafeString(currentRow.Cells[1].Value);
                txtTenNV.Text = ClassXuLyChung.GetSafeString(currentRow.Cells[2].Value);
                cbPhongBan.SelectedValue = ClassXuLyChung.GetSafeString(currentRow.Cells[7].Value); 
                cbChucVu.SelectedValue = ClassXuLyChung.GetSafeInt(currentRow.Cells[8].Value);
                cbDoUuTien.SelectedValue = ClassXuLyChung.GetSafeInt(currentRow.Cells[9].Value);
                DataTable dt = dm.loaddulieubyid(ClassXuLyChung.GetSafeInt(txtUserID.Text));

                object photoValue = dt.Rows[0]["ChuKy"];
                if (!Convert.IsDBNull(photoValue) && photoValue != null)
                {
                    pictureBoxIcon.Image = ClassXuLyChung.ConvertByeToImage((byte[])photoValue);//set image property of the picture box by creating a image from stream 
                    pictureBoxIcon.SizeMode = PictureBoxSizeMode.StretchImage;//set size mode property of the picture box to stretch 
                    pictureBoxIcon.Refresh();//refresh picture box
                }
                else
                {
                    pictureBoxIcon.Image = null;
                }

            }
            else
            {
                SoanLai();
                txtUserID.Text = "";
            }
        }
        private void SoanLai()
        {
            txtMaNV.Text = "";
            txtTenNV.Text = "";
          
            txtDuongDan.Text = "";
            pictureBoxIcon.Image = null;
           
        }
        private void trangthaisudung2button(bool trangthaidung)
        {
            btnThem.Enabled = trangthaidung;
            btnSua.Enabled = trangthaidung;
        }
        private void trangthaisudung1button(bool trangthaidung)
        {
            btnXoa.Enabled = trangthaidung;

        }
        private void trangthaisudung3button(bool trangthaidung)
        {
            btnLuu.Enabled = trangthaidung;
            btnKhong.Enabled = trangthaidung;
            btnNhapLai.Enabled = trangthaidung;
        }
        private void trangthaisudungcontrol(bool trangthaidung)
        {

            txtMaNV.Enabled = trangthaidung;
            txtTenNV.Enabled = trangthaidung;
            cbDoUuTien.Enabled = trangthaidung;
            cbPhongBan.Enabled= trangthaidung;
           cbChucVu.Enabled = trangthaidung;
            btnChonHinh.Enabled = trangthaidung;
         
            txtMaNV.Focus();
        }
        private void loadgrid()
        {
            this.radGridView1.DataSource = dm.loadgrid();
        }
        private void khoitaogrid()
        {
            this.radGridView1.EnableHotTracking = true;
            this.radGridView1.ShowFilteringRow = false;
            this.radGridView1.EnableFiltering = true;
            this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
            this.radGridView1.MasterTemplate.ShowFilteringRow = false;
            this.radGridView1.MasterTemplate.AutoExpandGroups = true;
            this.radGridView1.TableElement.RowHeight = 65;
            this.radGridView1.MasterTemplate.AllowAddNewRow = false;
            this.radGridView1.MasterTemplate.AutoGenerateColumns = false;
            this.radGridView1.MasterTemplate.AllowEditRow = true;
            this.radGridView1.MasterTemplate.AllowDeleteRow = false;
            this.radGridView1.MasterTemplate.EnableSorting = true;
            this.radGridView1.MasterTemplate.AllowAutoSizeColumns = true;
            this.radGridView1.AutoScroll = true;
            this.radGridView1.EnableAlternatingRowColor = true;

            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn()
            {
                FieldName = "UserID",
                Name = "UserID",
                HeaderText = "ID",
                Width = 50,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn);

            GridViewTextBoxColumn textBoxColumn1 = new GridViewTextBoxColumn()
            {
                FieldName = "MaNV",
                Name = "MaNV",
                HeaderText = "Mã Nhân Viên",
                Width = 165,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn1);

            GridViewTextBoxColumn textBoxColumn2 = new GridViewTextBoxColumn()
            {
                FieldName = "TenNV",
                Name = "TenNV",
                HeaderText = "Tên Nhân Viên",
                Width = 200,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn2);

            GridViewTextBoxColumn textBoxColumn3 = new GridViewTextBoxColumn()
            {
                FieldName = "TenPB",
                Name = "TenPB",
                HeaderText = "Phòng Ban",
                Width = 250,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn3);

            GridViewTextBoxColumn textBoxColumn4 = new GridViewTextBoxColumn()
            {
                FieldName = "ChucVu",
                Name = "ChucVu",
                HeaderText = "Chức Vụ",
                Width = 250,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn4);

            GridViewTextBoxColumn textBoxColumn5 = new GridViewTextBoxColumn()
            {
                FieldName = "DienGiai",
                Name = "DienGiai",
                HeaderText = "Độ Ưu Tiên",
                Width = 250,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn5);



            GridViewImageColumn imagecolumn = new GridViewImageColumn()
            {
                FieldName = "ChuKy",
                Name = "ChuKy",
                HeaderText = "Chữ Ký",
                Width = 65,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter,
                ImageLayout = ImageLayout.Stretch,
                ImageAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(imagecolumn);


            GridViewTextBoxColumn textBoxColumn6 = new GridViewTextBoxColumn()
            {
                FieldName = "MaPB",
                Name = "MaPB",
                HeaderText = "MaPB",
                Width = 150,
                IsVisible = false,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn6);

            GridViewTextBoxColumn textBoxColumn7 = new GridViewTextBoxColumn()
            {
                FieldName = "MaChucVu",
                Name = "MaChucVu",
                HeaderText = "MaChucVu",
                Width = 107,
                IsVisible = false,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn7);


            GridViewTextBoxColumn textBoxColumn8 = new GridViewTextBoxColumn()
            {
                FieldName = "DoUuTien",
                Name = "DoUuTien",
                HeaderText = "DoUuTien",
                Width = 107,
                IsVisible = false,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn8);

        }
        private void openfile()
        {
            OpenFileDialog OPF = new OpenFileDialog()
            {
                Title = "Open File Image",
                Filter = "Image File (.JPG,.PNG,.ICO)|*.jpg;*.png;*.ico"
            };
            if (OPF.ShowDialog() == DialogResult.OK)
            {
                txtDuongDan.Text = Path.GetFullPath(OPF.FileName).Trim();

            }
        }
        private void btnChonHinh_Click(object sender, EventArgs e)
        {
            openfile();
            if (txtDuongDan.Text != "")
            {
                pictureBoxIcon.Image = Image.FromFile(txtDuongDan.Text);
                pictureBoxIcon.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBoxIcon.Refresh();
            }
            else
            {
                pictureBoxIcon.Image = null;
            }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            ClassXuLyChung.tuychontimkiemtrenluoi(txtTimKiem, radGridView1, 0, 0);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            option = "Them";
            SoanLai();
            trangthaisudungcontrol(true);
            trangthaisudungluoi(false);
            trangthaisudung2button(false);
            trangthaisudung1button(false);
            trangthaisudung3button(true);
        }
        public void trangthaisudungluoi(bool trangthaidung)
        {
            radGridView1.Enabled = trangthaidung;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            option = "Sua";
            trangthaisudungcontrol(true);
            trangthaisudungluoi(true);
            trangthaisudung2button(false);
            trangthaisudung1button(false);
            trangthaisudung3button(true);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUserID.Text))
            {
                DialogResult kq = RadMessageBox.Show(String.Format("Bạn có muốn chắc xóa nhân viên \n : {0} không ???", txtMaNV.Text), "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (kq == DialogResult.Yes)
                {

                    if (dm.xoa(int.Parse(txtUserID.Text)) == 1)
                    {
                        RadMessageBox.Show("Xóa Thành Công nhân viên \n : " + txtMaNV.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                        SoanLai();
                        loadgrid();
                        UpdatePanelInfo(this.radGridView1.CurrentRow);
                    }
                    else
                    {
                        RadMessageBox.Show("nhân viên này không tồn tại ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                    }
                }

            }
            else
            {
                RadMessageBox.Show("Không tồn tại dữ liệu ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
            }
        }

        private void btnKhong_Click(object sender, EventArgs e)
        {

            option = "";
            trangthaisudungcontrol(false);
            trangthaisudungluoi(true);
            trangthaisudung2button(true);
            trangthaisudung1button(true);
            trangthaisudung3button(false);
            loadgrid();
            UpdatePanelInfo(this.radGridView1.CurrentRow);
        }

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            SoanLai();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMaNV.Text !="")
            {
                if (option == "Them")
                {
                    if (dm.them(txtMaNV.Text,txtTenNV.Text,ClassXuLyChung.GetSafeInt(cbDoUuTien.SelectedValue),ClassXuLyChung.GetSafeString(cbPhongBan.SelectedValue),txtDuongDan.Text,ClassXuLyChung.GetSafeInt(cbChucVu.SelectedValue)) == 1)
                    {
                        RadMessageBox.Show("Thêm Thành Công Nhân Viên : " + txtMaNV.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                    }
                    else
                    {
                        RadMessageBox.Show(String.Format("Nhân Viên: {0} đã tồn tại", txtMaNV.Text), "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                    }
                }
                else
                {
                    if (option == "Sua")
                    {
                        if (txtMaNV.Text != "")
                        {

                            if (dm.sua(txtMaNV.Text, txtTenNV.Text, ClassXuLyChung.GetSafeInt(cbDoUuTien.SelectedValue), ClassXuLyChung.GetSafeString(cbPhongBan.SelectedValue), txtDuongDan.Text, ClassXuLyChung.GetSafeInt(cbChucVu.SelectedValue),ClassXuLyChung.GetSafeInt(txtUserID.Text)) == 1)
                            {
                                RadMessageBox.Show("Cập Nhật Thành Công Nhân Viên : " + txtMaNV.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                            }
                            else
                            {
                                RadMessageBox.Show("Nhân Viên này đã tồn tại", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                            }
                        }
                        else
                        {
                            RadMessageBox.Show("Không tồn tại dữ liệu ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                        }
                    }
                }
                option = "";
                trangthaisudungcontrol(false);
                trangthaisudungluoi(true);
                trangthaisudung2button(true);
                trangthaisudung1button(true);
                trangthaisudung3button(false);
                SoanLai();
                loadgrid();
                UpdatePanelInfo(this.radGridView1.CurrentRow);

            }
            else
            {
                RadMessageBox.Show("Chưa Nhập Đầy Đủ Thông Tin. ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
            }
        }

        private void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
        {
            UpdatePanelInfo(this.radGridView1.CurrentRow); 
        }

    
     
    }
}
